<?php
/**
 * Created by jinqiang on 2024/7/12 17:16.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Produce;


use App\Http\Models\Mysql\SsyProductCategoryTable;

class SafetyService
{
    public function search($param)
    {
        $conn = (new SsyProductCategoryTable());
        if(is_array($param['types'])){
            $types = array_map("intval", $param['types']);
            $conn = $conn->whereIn('types', $types);
        }else{
            $conn = $conn->where('types', (Int)$param['types']);
        }
        $res = $conn->orderBy('createtime', 'desc')->paginate((Int)$param['limit'])->toArray();
        return [
            'data' => $res['data'],
            'total' => $res['total']
        ];
    }

    public function del($id)
    {
        return (new SsyProductCategoryTable())->deleteData((Int)$id);
    }

    public function save($data)
    {
        $data = json_decode($data, true);
        $id = $data['id'] ?? "";
        $data = [
            'types' => (Int)$data['types'],
            'title' => $data['title'] ?? "",
            'title_des' => $data['title_des'] ?? "",
            'createtime' => date('Y-m-d H:i:s')
        ];
        if(!empty($id)){
            unset($data['createtime']);
            return (new SsyProductCategoryTable())->updateData($id, $data);
        }else{
            return (new SsyProductCategoryTable())->createData($data);
        }
    }
}
